/**
 * @Auther: wangliuyang
 * @Date: 2021/4/19 22:30
 * @Description: 25. K 个一组翻转链表
 */
public class Solution_25 {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode hair = new ListNode(-1);
        hair.next = head;
        ListNode cur = hair;
        /*
        判断第k个节点是否存在
        */
        for (int i = 0; i < k; i++) {
            cur = cur.next;
            if (cur == null) {
                return hair.next;
            }
        }
        ListNode pre = reverseKGroup(cur.next, k);
        ListNode next = null;
        ListNode tmp = head;
        while (tmp != cur) {
            next = tmp.next;
            tmp.next = pre;
            pre = tmp;
            tmp = next;
        }
        tmp.next = pre;
        return tmp;
    }
}
